package com.base.po.coupon_sdk;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beust.jcommander.internal.Maps;
import com.base.helper.WebHelper;
import com.base.po.coupon_new.GoodsTypeResponse;
import com.base.po.coupon_new.GoodsTypeResponseData;
import com.base.utils.DateUtil;
import com.base.utils.Produce;
import org.apache.commons.collections.map.HashedMap;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 365优惠券
 */
public class CouponInfoUtilNew {

    /**
     * 查询品牌接口
     */
    public static List<GoodsTypeResponseData> goodsType() {
        List<GoodsTypeResponseData> responseDatas = null;

        try {
            Map<String, Object> map = new HashedMap();
            map.put("app_id", CouponConfigNew.APPID);
            map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

            String sign = CouponUtilNew.getSign(map, CouponConfigNew.TRADE_KEY).toUpperCase();// 签名
            String param = CouponUtilNew.getParam(map, sign.toUpperCase());// 参数
            String result = WebHelper.PostData(CouponConfigNew.GOODS_TYPE, param, null, null);
            GoodsTypeResponse response = JSON.toJavaObject(JSON.parseObject(result), GoodsTypeResponse.class);
            if (response != null && response.getCode() == 200) {
                CouponAESUtil aesUtil = new CouponAESUtil();
                String resultData = aesUtil.decrypt(response.getData(), CouponConfigNew.SECRET_KEY);
                responseDatas = new ArrayList<>(JSONArray.parseArray(resultData, GoodsTypeResponseData.class));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return responseDatas;
    }


    /**
     * @param brand_id     品牌ID，传次参数的时候根据品牌ID返回产品列表的值，不传入则返回全部
     * @param commodity_id 产品id，传此参数的时候根据ID返回产品信息，若品牌id同时存在则只根据产品ID查询
     * @return
     */
    public static String goodsGet(String brand_id, String commodity_id) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashedMap();
        map.put("app_id", CouponConfigNew.APPID);
        map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));

        String sign = CouponUtilNew.getSign(map, CouponConfigNew.TRADE_KEY).toUpperCase();// 签名
        String param = CouponUtilNew.getParam(map, sign.toUpperCase());// 参数
        String result = WebHelper.PostData(CouponConfigNew.GOODS_GET, param, null, null);
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(result);
        CouponAESUtil aesUtil = new CouponAESUtil();
        String resultData = aesUtil.decrypt(jsonObject.getString("data"), CouponConfigNew.SECRET_KEY);

        return resultData;
    }

    /**
     *
     * @param goodsNo   商品唯一编号
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getStock(String goodsNo) throws UnsupportedEncodingException {
        Map<String, Object> map = new HashedMap();
        map.put("app_id", CouponConfigNew.APPID);
        map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));
        map.put("goods_no",goodsNo);

        String sign = CouponUtilNew.getSign(map, CouponConfigNew.TRADE_KEY).toUpperCase();// 签名
        String param = CouponUtilNew.getParam(map, sign.toUpperCase());// 参数

        String result = WebHelper.PostData(CouponConfigNew.GOOD_STOCK, param, null, null);
        JSONObject jsonObject=JSONObject.parseObject(result);
        CouponAESUtil aesUtil = new CouponAESUtil();
        String resultData = aesUtil.decrypt(jsonObject.getString("data"), CouponConfigNew.SECRET_KEY);
        return resultData;
    }

    /**
     *
     * @param goodsId           商品ID
     * @param count             购买数量
     * @param batch_no          订单号ID
     * @param notify_url        通知地址
     * @param redirect_url      重定向地址
     * @param order_change_notify_url   订单状态变更通知
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getPayUrl(String goodsId,Integer count,String batch_no,String notify_url,String redirect_url,String order_change_notify_url) throws UnsupportedEncodingException {
        Map<String, Object> map = Maps.newHashMap();
        map.put("app_id", CouponConfigNew.APPID);
        map.put("time_stamp", String.valueOf(Produce.getNowDate().getTime()));
        map.put("goods_no", goodsId);
        map.put("count", count);
        map.put("batch_no", batch_no);
        map.put("notify_url", notify_url);
        map.put("redirect_url", redirect_url);
        map.put("order_change_notify_url", order_change_notify_url);


        String sign = CouponUtilNew.getSign(map, CouponConfigNew.TRADE_KEY).toUpperCase();// 签名
        String param = CouponUtilNew.getParam(map, sign.toUpperCase());// 参数
        String result = WebHelper.PostData(CouponConfigNew.PAY_GOODS, param, null, null);
        return result;
    }

}
